package com.laicunba.ext.service.impl;

import java.io.InputStream;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.laicunba.ext.service.JzhFtpService;
import com.laicunba.util.AssertUtil;

@Service
public class JzhFtpServiceImpl implements JzhFtpService {

	@Value("${jzh.ftp.host:ftp-1.fuiou.com}")
	private String ftpHost;
	@Value("${jzh.ftp.port:9022}")
	private String ftpPort;
	@Value("${jzh.ftp.username:JMS353029sftp}")
	private String ftpUserName;
	@Value("${jzh.ftp.password:F124a1avh69ry2Ty}")
	private String ftpPassword;

	@Override
	public void upload(InputStream inputStream, String destPath) {

		Channel channel = null;
		try {

			ChannelSftp sftp = null;
			JSch jsch = new JSch();
			Session session = jsch.getSession(ftpUserName, ftpHost, Integer.parseInt(ftpPort));
			Properties conf = new Properties();
			conf.put("StrictHostKeyChecking", "no");
			session.setConfig(conf);
			session.setPassword(ftpPassword);
			session.connect();
			channel = session.openChannel("sftp");
			channel.connect();
			sftp = (ChannelSftp) channel;
			sftp.put(inputStream, destPath);
			channel.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
			AssertUtil.assertFail(e.getMessage());
		} finally {
			if (channel != null) {
				channel.disconnect();
			}

		}

	}

}
